#ifndef TCPTRANSPORT_H
#define TCPTRANSPORT_H

#include <QThread>
#include <QTcpSocket>
#include <QMutex>
#include <QHostAddress>

#include "anyevent.h"

#include "wireformat.h"
#include "payload.h"

namespace apollo
{
	namespace core
	{
		class TcpTransport : public QThread
		{
			Q_OBJECT

		public:
			TcpTransport(QObject *parent);
			~TcpTransport();

			void stop(){ exit();}
signals:
			void errorEvent(QString ip, int port, QTcpSocket::SocketError socketError);
			void dataArrivedEvent(PayloadPtr payload);

		protected:
			virtual void run();
			virtual void customEvent(QEvent *event);

			private slots:
				void readDataHandler();
				void socketErrorHandler(QAbstractSocket::SocketError socketError);

		private:
			QSharedPointer<QTcpSocket> socket;
			WireFormat::ReadingContext ctx;
			QMutex locker;
			WireFormat format;

		};
	}}
#endif // TCPTRANSPORT_H
